﻿using System;
using System.Collections.Generic;

namespace Chess.Domain
{
	internal class BishopPositionDeterminant : IPositionDeterminant
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public List<Position> AvailablePositions(Position currentPosition)
        {
			List<Position> results = new List<Position>();
			results.AddRange(DetermineAllPositionsUpRight(currentPosition));
			results.AddRange(DetermineAllPositionsUpLeft(currentPosition));
			results.AddRange(DetermineAllPositionsDownRight(currentPosition));
			results.AddRange(DetermineAllPositionsDownLeft(currentPosition));

			return results;
        }


		#region " Helper Methods "

		/// <summary>
		/// 
		/// </summary>
		/// <param name="position"></param>
		/// <returns></returns>
		private List<Position> DetermineAllPositionsUpRight(Position currentPosition)
		{
			List<Position> results = new List<Position>();
			Position item;
			int deltaY = 1;
			int deltaX = 1;

			while (currentPosition.CanTranslate(deltaY, deltaX, out item))
			{
				results.Add(item);

				deltaX++;
				deltaY++;
			}

			return results;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="position"></param>
		/// <returns></returns>
		private List<Position> DetermineAllPositionsUpLeft(Position currentPosition)
		{
			List<Position> results = new List<Position>();
			Position item;
			int deltaY = 1;
			int deltaX = -1;

			while (currentPosition.CanTranslate(deltaY, deltaX, out item))
			{
				results.Add(item);

				deltaY++;
				deltaX--;
			}

			return results;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="position"></param>
		/// <returns></returns>
		private List<Position> DetermineAllPositionsDownRight(Position currentPosition)
		{
			List<Position> results = new List<Position>();
			Position item;
			int deltaY = -1;
			int deltaX = 1;

			while (currentPosition.CanTranslate(deltaY, deltaX, out item))
			{
				results.Add(item);

				deltaY--;
				deltaX++;
			}

			return results;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="position"></param>
		/// <returns></returns>
		private List<Position> DetermineAllPositionsDownLeft(Position currentPosition)
		{
			List<Position> results = new List<Position>();
			Position item;
			int deltaY = -1;
			int deltaX = -1;

			while (currentPosition.CanTranslate(deltaY, deltaX, out item))
			{
				results.Add(item);

				deltaY--;
				deltaX--;
			}

			return results;
		}

		#endregion
    }
}
